package logic

import (
	"context"
	"errors"
	"houses_srv/models"

	"houses_srv/houses"
	"houses_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddHouseLogic {
	return &AddHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddHouseLogic) AddHouse(in *houses.AddHouseRequest) (*houses.AddHouseResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var a models.AuthRealName
	a.UserId = int(in.UserId)
	err = a.ShowRealNameByUserId()
	if err != nil {
		logx.Error("房源发布失败", err)
		return nil, err
	}
	if a.ID == 0 {
		logx.Error("房源发布失败", errors.New("用户未实名"))
		return nil, errors.New("用户未实名")
	}

	var h models.House
	h.UserId = int(in.UserId)
	h.Title = in.Title
	h.Image = in.Image
	h.Price = int(in.Price)
	h.Fool = float64(in.Fool)
	h.HouseType = in.HouseType
	h.Status = 0
	err = h.AddHouse()
	if err != nil {
		logx.Error("房源发布失败")
		return nil, err
	}

	return &houses.AddHouseResponse{HouseId: int64(h.ID)}, nil
}
